using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using LeisureCenter.Api;

namespace LeisureCenter.Forms
{
    public partial class OsdForm : Form
    {
        public OsdForm()
        {
            InitializeComponent();
        }

        private void OsdForm_Load(object sender, EventArgs e)
        {
            Padding p = Program.LeisureCenterInstance.Settings.Padding;
            Padding = new Padding(p.Left + 20, p.Top + 20, p.Right + 20, p.Bottom + 20);
            Program.LeisureCenterInstance.PlaylistManager.PlayStateChanged += new EventHandler<LeisureCenter.Api.PlayStateChangeEventArgs>(PlaylistManager_PlayStateChanged);
        }

        void PlaylistManager_PlayStateChanged(object sender, PlayStateChangeEventArgs e)
        {
            switch (e.NewPlayState)
            {
                case MediaPlayState.Stopped:
                    Hide();
                    break;
                case MediaPlayState.Paused:
                    ShowOsd("Pause");
                    break;
                case MediaPlayState.Playing:
                    if (e.PreviousPlayState == MediaPlayState.Playing || e.PreviousPlayState == MediaPlayState.Stopped)
                        Hide();
                    else
                        ShowOsd("Play", 3);
                    break;
                case MediaPlayState.FastForward:
                    ShowOsd("Fast Forward " + Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.PlayRate.ToString(CultureInfo.InvariantCulture));
                    break;
                case MediaPlayState.FastReverse:
                    ShowOsd("Fast Reverse " + Math.Abs(Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.PlayRate).ToString(CultureInfo.InvariantCulture));
                    break;
            }
        }

        private void OsdForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Program.LeisureCenterInstance.PlaylistManager.PlayStateChanged -= new EventHandler<LeisureCenter.Api.PlayStateChangeEventArgs>(PlaylistManager_PlayStateChanged);
        }

        internal void ShowOsd(string caption)
        {
            if (Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.FullScreen)
            {
                lblCaption.Text = caption;
                pnlCaption.Visible = !string.IsNullOrEmpty(caption);
                pnlCaption.Width = lblCaption.Width + 20;
                Show();
            }
        }

        internal void ShowOsd(string caption, int hideDelay)
        {
            ShowOsd(caption);
            tmHide.Interval = hideDelay * 1000;
            tmHide.Start();
        }

        private void tmHide_Tick(object sender, EventArgs e)
        {
            Hide();
            tmHide.Stop();
        }

        internal void ShowAfterFullScreen()
        {
            switch (Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.PlayState)
            {
                case MediaPlayState.Stopped:
                    Hide();
                    break;
                case MediaPlayState.Paused:
                    ShowOsd("Pause");
                    break;
                case MediaPlayState.FastForward:
                    ShowOsd("Fast Forward " + Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.PlayRate.ToString(CultureInfo.InvariantCulture));
                    break;
                case MediaPlayState.FastReverse:
                    ShowOsd("Fast Reverse " + Math.Abs(Program.LeisureCenterInstance.PlaylistManager.Playlist.ActivePlaylistItemOrStub.PlayRate).ToString(CultureInfo.InvariantCulture));
                    break;
            }
        }
    }
}